/*******************************************************************************
* @Copyright: Shenzhen Hangshun Chip Technology R&D Co., Ltd 
* @Filename : hk32c030xx_ts.h
* @brief    : This file contains all the functions prototypes for the TS firmware 
  *          library
* @Author   : AE Team 
*******************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __HK32C030XX_TS_H
#define __HK32C030XX_TS_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "hk32c030xx_conf.h"

/** @addtogroup TS
  * @{
  */

/** @defgroup TS_Exported_Types  TS_Exported_Types
  * @{
  */
/* Exported types ------------------------------------------------------------*/

/** 
  * @brief  TS Init structure definition
  */
  
typedef struct
{
  uint32_t TS_ClockDIV;                  /*!< Selects the clock division of the temperature sensor.
                                                 This parameter can be a value of @ref TS_ClockDIV */

  uint32_t TS_ANASTUP;   								 /*!< Analog circuit start up time of the temperature sensor.
                                                 This parameter can be set 1 to 1.875us. */

  uint32_t TS_SMPDLY;        						 /*!< Sample delay of the temperature sensor.
																									This parameter can be set 1 to 16 TS_Clock cycles*/

  uint32_t TS_ALARM;           					 /*!< Alarm enable . This parameter
                                                 can be a value of @ref TS_ALARM_High_EN,TS_ALARM_Low_EN,TS_ALARM_Low_High_BothEN or TS_ALARM_Disable */

  uint32_t TS_EXTEN;                   		/*!< Selects the external trigger Edge and enables the
                                                 trigger of conversion. This parameter can be a value
                                                 of @ref TS_external_trigger_edge_conversion  */

  uint32_t  TS_EXTSEL;              			/*!< Defines the external trigger used to start the analog
                                                 to digital conversion of conversion. This parameter
                                                 can be a value of @ref TS_external_trigger_sources_for_conversion */
	
  uint32_t  TS_AVERAGE;             			 /*!< Average times. 
                                                 This parameter can be a value of @ref TS_AVERAGE */			
  uint16_t  TS_LOW_ALARM_TH;             			 /*!< Low temp threshold. 
                                                 This parameter can be a value of 9bits */	
  uint16_t  TS_HIGH_ALARM_TH;             			 /*!< High temp threshold. 
                                                 This parameter can be a value of 9bits */
																								 
}TS_InitTypeDef;

/**
  * @}
  */

/* Exported constants --------------------------------------------------------*/

/** @defgroup TS_Exported_Constants     TS_Exported_Constants
  * @{
  */


/** @defgroup TS_ClockDIV           TS_ClockDIV
  * @{
  */ 
#define TS_ClockDIV0                ((uint32_t)0x00000000)   /*!< TS_clock = 16MHZ */
#define TS_ClockDIV2			   				TS_CR_DIV_0 /*!< TS_clock = 8MHZ  */
#define TS_ClockDIV4               	TS_CR_DIV_1  /*!<  TS_clock = 4MHZ */
#define TS_ClockDIV8               	(TS_CR_DIV_0 | TS_CR_DIV_1)   /*!<  TS_clock = 2MHZ */
#define TS_ClockDIV16               TS_CR_DIV_2  /*!<  TS_clock = 1MHZ */
#define IS_TS_CLOCKMODE(CLOCK) (((CLOCK) == TS_ClockDIV0) ||\
	                               ((CLOCK) == TS_ClockDIV2) ||\
				                         ((CLOCK) == TS_ClockDIV4) ||\
																 ((CLOCK) == TS_ClockDIV8) ||\
				                         ((CLOCK) == TS_ClockDIV16))
/**
  * @}
  */ 

/** @defgroup TS_ANASTUP           TS_ANASTUP
  * @{
  */ 
#define TS_ANASTUP0               ((uint32_t)0x00000000)   /*!< Analog start up time = 1us */
#define TS_ANASTUP1			   				TS_CR_ANASTUP_0 /*!< Analog start up time = 1.125us  */
#define TS_ANASTUP2               TS_CR_ANASTUP_1  /*!<  Analog start up time = 1.25us */
#define TS_ANASTUP3               (TS_CR_ANASTUP_0 | TS_CR_ANASTUP_1)   /*!<  Analog start up time = 1.375us */
#define TS_ANASTUP4               TS_CR_ANASTUP_2  /*!<  Analog start up time = 1.5us */
#define TS_ANASTUP5               (TS_CR_ANASTUP_0 | TS_CR_ANASTUP_2)  /*!<  Analog start up time = 1.625us */
#define TS_ANASTUP6               (TS_CR_ANASTUP_1 | TS_CR_ANASTUP_2)  /*!<  Analog start up time = 1.75us */
#define TS_ANASTUP7               (TS_CR_ANASTUP_0 |TS_CR_ANASTUP_1 | TS_CR_ANASTUP_2) /*!<  Analog start up time = 1.875us */
#define IS_TS_ANASTUP(ANASTUP) (((ANASTUP) == TS_ANASTUP0) ||\
	                               ((ANASTUP) == TS_ANASTUP1) ||\
				                         ((ANASTUP) == TS_ANASTUP2) ||\
																 ((ANASTUP) == TS_ANASTUP3) ||\
																 ((ANASTUP) == TS_ANASTUP4) ||\
																 ((ANASTUP) == TS_ANASTUP5) ||\
																 ((ANASTUP) == TS_ANASTUP6) ||\
				                         ((ANASTUP) == TS_ANASTUP7))
/**
  * @}
  */ 

/** @defgroup TS_SMPDLY           TS_SMPDLY
  * @{
  */ 
#define TS_SMPDLY1			   			  ((uint32_t)0x00000000) /*!< Sample delay time = 1 cycle  */
#define TS_SMPDLY2                TS_CR_SMPDLY_0  /*!<  Sample delay time = 2 cycles */
#define TS_SMPDLY3                TS_CR_SMPDLY_1   /*!<  Sample delay time = 3 cycles */
#define TS_SMPDLY4                (TS_CR_SMPDLY_0 | TS_CR_SMPDLY_1)  /*!< Sample delay time = 4 cycles */
#define TS_SMPDLY5                TS_CR_SMPDLY_2  /*!<  Sample delay time = 5 cycles */
#define TS_SMPDLY6                (TS_CR_SMPDLY_0 | TS_CR_SMPDLY_2)  /*!<  Sample delay time = 6 cycles */
#define TS_SMPDLY7                (TS_CR_SMPDLY_1 | TS_CR_SMPDLY_2) /*!<  Sample delay time = 7 cycles */
#define TS_SMPDLY8			   			  (TS_CR_SMPDLY_0 |TS_CR_SMPDLY_1 | TS_CR_SMPDLY_2) /*!< Sample delay time = 8 cycles  */
#define TS_SMPDLY9               	TS_CR_SMPDLY_3  /*!<  Sample delay time = 9 cycles */
#define TS_SMPDLY10               (TS_CR_SMPDLY_0|TS_CR_SMPDLY_3)   /*!<  Sample delay time = 10 cycles */
#define TS_SMPDLY11               (TS_CR_SMPDLY_1|TS_CR_SMPDLY_3)  /*!<  Sample delay time = 11 cycles */
#define TS_SMPDLY12               (TS_CR_SMPDLY_0 |TS_CR_SMPDLY_1 | TS_CR_SMPDLY_3)  /*!<  Sample delay time = 12 cycles */
#define TS_SMPDLY13               (TS_CR_SMPDLY_2 | TS_CR_SMPDLY_3)  /*!<  Sample delay time = 13 cycles */
#define TS_SMPDLY14               (TS_CR_SMPDLY_0 |TS_CR_SMPDLY_2 | TS_CR_SMPDLY_3) /*!<  Sample delay time = 14 cycles */
#define TS_SMPDLY15               (TS_CR_SMPDLY_1 | TS_CR_SMPDLY_2| TS_CR_SMPDLY_3)  /*!<  Sample delay time = 15 cycles */
#define TS_SMPDLY16               (TS_CR_SMPDLY_0 |TS_CR_SMPDLY_1 | TS_CR_SMPDLY_2| TS_CR_SMPDLY_3) /*!<  Sample delay time = 16 cycles */

#define IS_TS_SMPDLY(SMPDLY) (((SMPDLY) == TS_SMPDLY1) ||\
	                               ((SMPDLY) == TS_SMPDLY2) ||\
				                         ((SMPDLY) == TS_SMPDLY3) ||\
																 ((SMPDLY) == TS_SMPDLY4) ||\
																 ((SMPDLY) == TS_SMPDLY5) ||\
																 ((SMPDLY) == TS_SMPDLY6) ||\
																 ((SMPDLY) == TS_SMPDLY7) ||\
																 ((SMPDLY) == TS_SMPDLY8) ||\
																 ((SMPDLY) == TS_SMPDLY9) ||\
																 ((SMPDLY) == TS_SMPDLY10) ||\
																 ((SMPDLY) == TS_SMPDLY11) ||\
																 ((SMPDLY) == TS_SMPDLY12) ||\
																 ((SMPDLY) == TS_SMPDLY13) ||\
																 ((SMPDLY) == TS_SMPDLY14) ||\
																 ((SMPDLY) == TS_SMPDLY15) ||\
				                         ((SMPDLY) == TS_SMPDLY16))
/**
  * @}
  */ 

/** @defgroup TS_ALARM     TS_ALARM
  * @{
  */ 
#define TS_ALARM_Disable              ((uint32_t)0x00000000)//Alarm disable
#define TS_ALARM_Low_EN               TS_CR_ALARM_0
#define TS_ALARM_High_EN              TS_CR_ALARM_1
#define TS_ALARM_Low_High_BothEN      TS_CR_ALARM

#define IS_TS_ALARM(ALARM) (((ALARM) == TS_ALARM_Disable) || \
                                    ((ALARM) == TS_ALARM_Low_EN) || \
                                    ((ALARM) == TS_ALARM_High_EN) || \
                                    ((ALARM) == TS_ALARM_Low_High_BothEN))
/**
  * @}
  */ 

/** @defgroup TS_external_trigger_edge_conversion     TS_external_trigger_edge_conversion
  * @{
  */ 
#define TS_ExternalTrigConvEdge_None              ((uint32_t)0x00000000)//Sofware Trigger
#define TS_ExternalTrigConvEdge_Rising            TS_CR_EXTEN_0
#define TS_ExternalTrigConvEdge_Falling           TS_CR_EXTEN_1
#define TS_ExternalTrigConvEdge_RisingFalling     TS_CR_EXTEN

#define IS_TS_EXT_TRIG_EDGE(EDGE) (((EDGE) == TS_ExternalTrigConvEdge_None) || \
                                    ((EDGE) == TS_ExternalTrigConvEdge_Rising) || \
                                    ((EDGE) == TS_ExternalTrigConvEdge_Falling) || \
                                    ((EDGE) == TS_ExternalTrigConvEdge_RisingFalling))
/**
  * @}
  */ 

/** @defgroup TS_external_trigger_sources_for_conversion      TS_external_trigger_sources_for_conversion
  * @{
  */ 

/* TIM1 */
#define TS_ExternalTrigConv_T1_TRGO               ((uint32_t)0x00000000) /*0*/
#define TS_ExternalTrigConv_T1_CC4              	 TS_CR_EXTSEL_2  /*4*/
#define TS_ExternalTrigConv_T1_CC5                ((uint32_t)(TS_CR_EXTSEL_0 | TS_CR_EXTSEL_2)) /*5*/
#define TS_ExternalTrigConv_T1_CC6                ((uint32_t)(TS_CR_EXTSEL_1 | TS_CR_EXTSEL_2))  /*6*/
/* TIM2 */
#define TS_ExternalTrigConv_T2_TRGO                TS_CR_EXTSEL_0   /*1*/
/* TIM3 */
#define TS_ExternalTrigConv_T3_TRGO                TS_CR_EXTSEL_1   /*2*/
/* TIM15 */
#define TS_ExternalTrigConv_T15_TRGO               ((uint32_t)(TS_CR_EXTSEL_0 | TS_CR_EXTSEL_1))  /*3*/
/* RTC Trig */
#define TS_ExternalTrigConv_RTC_TRGO               ((uint32_t)(TS_CR_EXTSEL_0 | TS_CR_EXTSEL_1 | TS_CR_EXTSEL_2))   /*7*/

#define IS_TS_EXTERNAL_TRIG_CONV(CONV) (((CONV) == TS_ExternalTrigConv_T1_TRGO) || \
                                         ((CONV) == TS_ExternalTrigConv_T1_CC4)   || \
                                         ((CONV) == TS_ExternalTrigConv_T2_TRGO)   || \
                                         ((CONV) == TS_ExternalTrigConv_T3_TRGO)   || \
                                         ((CONV) == TS_ExternalTrigConv_T15_TRGO)   || \
                                         ((CONV) == TS_ExternalTrigConv_T1_CC5)  || \
                                         ((CONV) == TS_ExternalTrigConv_RTC_TRGO) || \
                                         ((CONV) == TS_ExternalTrigConv_T1_CC6))
/**
  * @}
  */ 

/** @defgroup TS_AVERAGE           TS_AVERAGE
  * @{
  */ 
#define TS_AVERAGE0               ((uint32_t)0x00000000)   /*!< Average times = 0 */
#define TS_AVERAGE2			   				TS_CR_AVERAGE_0 /*!< Average times = 2  */
#define TS_AVERAGE4               TS_CR_AVERAGE_1  /*!<  Average times = 4 */
#define TS_AVERAGE8               (TS_CR_AVERAGE_0 | TS_CR_AVERAGE_1)   /*!<  Average times = 8 */
#define TS_AVERAGE16               TS_CR_AVERAGE_2  /*!<  Average times = 16 */
#define IS_TS_AVERAGE(AVERAGE) (((AVERAGE) == TS_AVERAGE0) ||\
				                         ((AVERAGE) == TS_AVERAGE2) ||\
																 ((AVERAGE) == TS_AVERAGE4) ||\
																 ((AVERAGE) == TS_AVERAGE8) ||\
				                         ((AVERAGE) == TS_AVERAGE16))
/**
  * @}
  */ 

/** @defgroup TS_interrupts_definition     TS_interrupts_definition
  * @{
  */ 
  
#define TS_IT_OVRTRIGO                           TS_IER_TRIGOVFIE
#define TS_IT_HIGH                               TS_IER_HTALARMIE
#define TS_IT_LOW                                TS_IER_LTALARMIE
#define TS_IT_SENDN                              TS_IER_SENDNIE
#define TS_IT_CALDN                              TS_IER_CALDNIE

#define IS_TS_GET_IT(IT) (((IT) == TS_IT_OVRTRIGO) || ((IT) == TS_IT_HIGH) || \
                           ((IT) == TS_IT_LOW)   || ((IT) == TS_IT_SENDN) || \
                           ((IT) == TS_IT_CALDN))

/**
  * @}
  */ 

/** @defgroup TS_flags_definition    TS_flags_definition
  * @{
  */ 
  
#define TS_FLAG_ANARDY                            TS_CR_ANARDY
#define TS_FLAG_OVRTRIGO                          TS_ISR_TRIGOVF
#define TS_FLAG_HIGH                              TS_ISR_HIGHT
#define TS_FLAG_LOW                               TS_ISR_LOWT
#define TS_FLAG_SENDN                             TS_ISR_SENDNE
#define TS_FLAG_CALDN                             TS_ISR_CALDNE

#define IS_TS_GET_FLAG(FLAG) (((FLAG) == TS_FLAG_ANARDY)   || ((FLAG) == TS_FLAG_OVRTRIGO) || \
                               ((FLAG) == TS_FLAG_HIGH)     || ((FLAG) == TS_FLAG_LOW) || \
                               ((FLAG) == TS_FLAG_SENDN)     || ((FLAG) == TS_FLAG_CALDN))
/**
  * @}
  */

/** @defgroup TS_flags_clear_definition    TS_flags_clear_definition
  * @{
  */ 
  
#define TS_FLAG_CLEAR_OVRTRIGO                          TS_ISR_TRIGOVCF
#define TS_FLAG_CLEAR_HIGH                              TS_ISR_HIGHTCF
#define TS_FLAG_CLEAR_LOW                               TS_ISR_LOWTCF
#define TS_FLAG_CLEAR_SENDN                             TS_ISR_SENDNCF
#define TS_FLAG_CLEAR_CALDN                             TS_ISR_CALDNCF

#define IS_TS_CLEAR_FLAG(CF_FLAG) (((CF_FLAG) == TS_FLAG_CLEAR_OVRTRIGO)   || ((CF_FLAG) == TS_FLAG_CLEAR_HIGH) || \
                               ((CF_FLAG) == TS_FLAG_CLEAR_LOW)     || ((CF_FLAG) == TS_FLAG_CLEAR_SENDN) || \
                               ((CF_FLAG) == TS_FLAG_CLEAR_CALDN))
/**
  * @}
  */


/**
  * @}
  */ 

/** @defgroup TS_Exported_Functions TS_Exported_Functions
  * @{
  */   
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */ 

/*  Function used to set the TS configuration to the default reset state *****/
void TS_DeInit(void);

/* Initialization and Configuration functions *********************************/ 
void TS_Init(TS_InitTypeDef* TS_InitStruct);
void TS_StructInit(TS_InitTypeDef* TS_InitStruct);
void TS_ANASTUP(uint32_t TS_start_times);
void TS_AMPDLY(uint32_t TS_samp_dly);
void TS_TRGO_EXTSEL(uint32_t Trigger_edge,uint32_t Trigger_sources);
void TS_THR_Config(uint16_t High_threshold,uint16_t Low_threshold);
void TS_ALARM_Cmd(uint32_t TS_ALARM);
void TS_Cmd(FunctionalState NewState);
void TS_Start(void);

/* Interrupts and flags management functions **********************************/
void TS_ITConfig(uint32_t TS_IT, FunctionalState NewState);
FlagStatus TS_GetFlagStatus(uint32_t TS_FLAG);
void TS_ClearFlag(uint32_t TS_FLAG);

/*TS Clock prescaler functions***************************************************/
void TS_SetClock(uint32_t TS_Clock_DIV);

/*TS oversampling functions***************************************************/
void TS_AVERAGE(uint32_t TS_Average_times);

/*TS get conversion value functions***************************************************/
uint16_t TS_GetConversionValue(void);

/*TS calibration functions***************************************************/
void TS_TESTMP_Config(uint16_t Cal_Temp);
void TS_CALR_Config(uint16_t Calibration);
uint16_t TS_GetCalibrationFactor(uint16_t Cal_Temp);
/**
  * @}
  */
 
/**
  * @}
  */




#ifdef __cplusplus
}
#endif

#endif /*__HK32C030XX_TS_H */


